package com.tmsps.ne4Weixin.utils;

import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.util.Map;

import com.alibaba.fastjson.JSON;

/**
 * 
 * @author zhangwei 396033084@qq.com BeanUtil
 */
public final class BeanUtil {

	/**
	 * @author zhangwei 396033084@qq.com 判断对象是否为null
	 */
	public static boolean isNull(Object object) {
		return null == object;
	}

	/**
	 * @author zhangwei 396033084@qq.com 判断对象是否不为null
	 */
	public static boolean nonNull(Object object) {
		return null != object;
	}

	@SuppressWarnings("unchecked")
	public static <T> T MapToBean(final Class<T> clazz, Map<String, String> map) {
		if (map == null || map.isEmpty()) {
			return null;
		}
		try {
			Object objBean = clazz.newInstance();
			BeanInfo beanInfo = Introspector.getBeanInfo(clazz);
			PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
			for (PropertyDescriptor property : propertyDescriptors) {
				String key = property.getName().toLowerCase();
				if (map.containsKey(key)) {
					Object value = map.get(key);
					// 得到property对应的setter方法
					Method setter = property.getWriteMethod();
					setter.invoke(objBean, value);
				}
			}
			return (T) objBean;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
}
